<?php
/**
 * Copyright (c) 2018,2345
 * 摘    要：创造程序
 * 作    者：林幸亿
 * 修改日期：2018.04.20
 */

require_once 'libraries/MysqlDbClass.php';
require_once 'libraries/RedisDbClass.php';

//Form submission
if (! empty($_POST))
{
    // keep track validation errors
    $nameError = null;
    $emailError = null;
    $mobileError = null;

    // keep track post values
    $name = $_POST['name'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];

    // validate input
    $valid = true;
    if (empty($name))
    {
        $nameError = '请输入名称<br>Please enter Name';
        $valid = false;
    }

    if (empty($email))
    {
        $emailError = '请输入电子邮件<br>Please enter Email Address';
        $valid = false;
    }
    else if (! filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        $emailError = '请输入有效的电子邮件<br>Please enter a valid Email Address';
        $valid = false;
    }

    if (empty($mobile))
    {
        $mobileError = '请输入手机号码<br>Please enter Mobile Number';
        $valid = false;
    }

    // Insert data if validation is successful and redirect back to main page
    if ($valid)
    {
        /*
        $db = MysqlDb::getInstance();
        $sql = "INSERT INTO users (name,email,mobile) values('" . $name . "','" . $email . "','" . $mobile . "')";
        $db->insert($sql);
        */      

        $redis = RedisDb::getInstance();
        $userId = $redis->incr('user:id');
        $userKey = 'user:' . $userId;

        $userArr = [
                    'name' => $name,
                    'email' => $email, 
                    'mobile' => $mobile
                    ];


        $redis->hmset($userKey, $userArr);
        //$result = $redis->set('user:username:' .$name, 1); //$user_id


        $userArr = ['name', 'email', 'mobile'];

        $test = $redis->hmget($userKey, $userArr);
        
        echo $userKey . '<br>';
        die(print_r($test));
  
        //Redirect back to main page
        header("Location: index.php");
    }
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <link   href="css/bootstrap.min.css" rel="stylesheet">
    </head>

    <body>
        <div class="container">

            <div class="span10 offset1">
                <div class="row">
                    <h3>插入用户 Create a User</h3>
                </div>

                <form class="form-horizontal" action="create.php" method="post">
                    <div class="control-group <?php echo !empty($nameError) ? 'error' : ''; ?>">
                        <label class="control-label">姓名 Name</label>
                        <div class="controls">
                            <input name="name" type="text"  placeholder="Name" value="<?php echo !empty($name) ? $name : ''; ?>">
                            <?php if (!empty($nameError)): ?>
                                <span class="help-inline"><?php echo $nameError; ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="control-group <?php echo !empty($emailError) ? 'error' : ''; ?>">
                        <label class="control-label">电子邮件 Email Address</label>
                        <div class="controls">
                            <input name="email" type="text" placeholder="Email Address" value="<?php echo !empty($email) ? $email : ''; ?>">
                            <?php if (! empty($emailError)): ?>
                                <span class="help-inline"><?php echo $emailError; ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="control-group <?php echo!empty($mobileError) ? 'error' : ''; ?>">
                        <label class="control-label">手机号码 Mobile Number</label>
                        <div class="controls">
                            <input name="mobile" type="text"  placeholder="Mobile Number" value="<?php echo !empty($mobile) ? $mobile : ''; ?>">
                            <?php if (! empty($mobileError)): ?>
                                <span class="help-inline"><?php echo $mobileError; ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="form-actions">
                        <button type="submit" class="btn btn-success">提交 Create</button>
                        <a class="btn" href="index.php">返回 Back</a>
                    </div>
                </form>
            </div>

        </div> <!-- /container -->
    </body>
</html>